﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Reflection;  

namespace CochinitaCorte
{
    public class FTPManager 
    {
        private WebException Err;
        

        private string FileData;
        private string ftpName;
        
        public event ErrorEventHandler OnError;

        public FTPManager()
        {
            try
            {
          //      ftp = db.FTPServers.FirstOrDefault();
          //      ftpName = "ftp://" + ftp.HostName + "/" + Resources.UploadPath + "/";
            }
            catch
            { }  
        }

        public string GetFileData(string FileName)
        {
            Uri uri;
           
                uri = new Uri(ftpName + FileName);

                if (!GetFileFromServer(uri))
                { int x = 0; }
                    // StatusBarInfo.Instance.SendStatusInfo(MessageError.FTPError);

            return FileData;
        }

        private bool GetFileFromServer(Uri serverUri)
        {
            if (serverUri.Scheme != Uri.UriSchemeFtp)
            {
                return false;
            }

            WebClient request = new WebClient();
    //        request.Credentials = new NetworkCredential(ftp.UserName, ftp.Password);

                ASCIIEncoding ascii = new ASCIIEncoding();
                byte[] newFileData = request.DownloadData(serverUri.ToString());
                FileData = ascii.GetString(newFileData);

            return true;
        }

        public List<string> GetFileList()
        {
            List<string> results = new List<string>();  
            FtpWebRequest request;
            FtpWebResponse response;
            Stream responseStream;
            StreamReader reader;

             
                request = createFTPConnection();
                request.Method = WebRequestMethods.Ftp.ListDirectory;

                response = (FtpWebResponse)request.GetResponse();
                responseStream = response.GetResponseStream();
                reader = new StreamReader(responseStream);

                while (!reader.EndOfStream)
                    results.Add(reader.ReadLine());

                reader.Close();
                response.Close();
           
            return results;
        }

        private FtpWebRequest createFTPConnection(string FileName = "")
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpName + Path.GetFileName(FileName));
         //   request.Credentials = new NetworkCredential(ftp.UserName, ftp.Password);
            request.Timeout = 5000;
            return request;
        }

        public bool uploadFile(string FileName)
        {
            FtpWebRequest request;
            FileStream stream;
            Stream reqStream;

                request = createFTPConnection(Path.GetFileName(FileName));
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = true;

                stream = File.OpenRead(FileName);
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                stream.Close();

                reqStream = request.GetRequestStream();
                reqStream.Write(buffer, 0, buffer.Length);
                reqStream.Flush();
                reqStream.Close();
                return true;
        }
         
    }
}